package com.cqc.interpolator01;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Path;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;

/**
 * 获取插值器 Interpolator
 * 获取path
 * 创建属性动画
 * 设置动画
 * 开启动画
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;
    private View view;
    private ObjectAnimator objectAnimator;
    private Path path1;
    private Interpolator interpolator1;
//    private OvershootInterpolator interpolator1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        view = findViewById(R.id.view);

//        OvershootInterpolator overshoot = new OvershootInterpolator(1.0f);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);


        String a = "测试6";
    }

    private void createInterpolatorByXml() {
        interpolator1 = AnimationUtils.loadInterpolator(MainActivity.this, android.R.interpolator.linear);
//        interpolator1 = (OvershootInterpolator) AnimationUtils.loadInterpolator(MainActivity.this, android.R.interpolator.overshoot);

        path1 = new Path();
        path1.moveTo(1.0f, 1.0f);
        path1.lineTo(0.2f, 0.2f);

//        objectAnimator = ObjectAnimator.ofFloat(view, View.SCALE_X, View.SCALE_Y, path1);//API>=21
        ObjectAnimator.ofFloat(view, View.SCALE_X, 0.2f, 1.0f);
        objectAnimator.setDuration(2000);
        objectAnimator.setInterpolator(interpolator1);
        objectAnimator.start();
    }


    private void createInterpolatorByJava() {
//        finish();
//        startActivity(new Intent(MainActivity.this, MainActivity2.class));

        BounceInterpolator interpolator2 = new BounceInterpolator();
        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(view, View.SCALE_X, 0.2f, 1.0f);
        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(view, View.SCALE_Y, 0.2f, 1.0f);

        AnimatorSet set = new AnimatorSet();
        set.playTogether(objectAnimatorX, objectAnimatorY);
        set.setInterpolator(interpolator2);
        set.setDuration(2000);
        set.start();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                createInterpolatorByXml();
                break;
            case R.id.btn2:
                createInterpolatorByJava();
                break;
        }
    }

    // 常量

//        View.TRANSLATION_X;
//        View.TRANSLATION_Y;
//        View.TRANSLATION_Z;
//        View.SCALE_X;
//        View.SCALE_Y;
//        View.ALPHA;
//        View.ROTATION;
//        View.ROTATION_X;
//        View.ROTATION_Y;


//        android.R.interpolator.bounce;
//        android.R.interpolator.overshoot;
//        android.R.interpolator.anticipate_overshoot;
//        android.R.interpolator.bounce;
//        android.R.interpolator.bounce;
//        android.R.interpolator.bounce;
}


